<?php
class Categories extends MY_Controller
{
  var $curl = 'categories';
  var $name = '';
  var $search = '';
  var $language;
  var $submenu = "_menu"; 
  var $id_category = '';
  
  function __construct()
  {
    parent::__construct();
        
    $this->load->model('/public/category_model');

    $this->validation->set_fields($this->category_model->fields);

    $this->data['Title'] = T_('Test Categories');

    $this->language = parent::getLang();
    
    $this->name = T_('Test Categories');   
  }
  
  function index()
  {
    $this->category_model->search();
    $this->search = $this->data['search'];
    
    $this->renderLayout();
  }

  function create()
  {
    $this->data['Title'] = $this->name . ' / ' . T_('Create');
    if (count($_POST))
    {           
      $rules = array('name', 'description');
      $this->validation->set_rules($this->category_model->getRules($rules));

      $this->validation->set_message('__nameUnique', '<span class="black bold">«Категория»</span> с таким названием уже существует! Измените название.');

      if ($this->validation->run() === TRUE)
      {
        $item['name']       = $this->validation->name;
        $item['description']= $this->validation->description;
        
        if ($this->category_model->insert($item))
        {
          redirect($this->curl . '/index'); 
        }       
      }
    }

    $this->renderLayout();    
  }
  
  function edit($id = null)
  {
    if (!empty($id))
    {     
      $object = $this->category_model->get($id);
      $this->data['Title'] = $object['name'] . ' / ' . T_('Edit');
          
      if (!count($object))
      {
        $this->data['view'] = '_elements/query_no_object';
        $this->load->view($this->data['layout'], $this->data);
  
        return false; 
      }
    
      if (!count($_POST))
      {
        $this->validation->id     = $object['id']; 
        $this->validation->name   = $object['name'];
        $this->validation->description   = $object['description'];
      }
      else
      {
        $this->id_category = $id;
        
        $rules = array('name', 'description');
        $this->validation->set_rules($this->category_model->getRules($rules));
  
        $this->validation->set_message('__nameUnique', '<span class="black bold">«Категория»</span> с таким названием уже существует! Измените название.');
        
        if ($this->validation->run() === TRUE)
        {
          $item['id']   = (int)$this->validation->id;
          $item['name'] = $this->validation->name;
          $item['description'] = $this->validation->description;
          
          $this->category_model->update($item);

          redirect($this->curl . '/index');
        }
      }     
    }
    
    $this->renderLayout();
  }
  
  function deleteItems()
  {
    $data = $this->category_model->delete($_REQUEST['items']);

    echo $data;
  }

  function delete($id)
  {
    $data = $this->category_model->delete($id);

    if ($data)
    {
      redirect($this->curl . '/index'); 
    }
    else
    {
      redirect($this->curl . '/edit/'. $id);
    }
  }
    
  function __nameUnique($name)
  {   
    return $this->category_model->nameUnique($name);
  } 
}

?>